/**   
* @Title: HttpClassLoad.java 
* @Package cn.com.ytst.data.integration.util 
* @Description: 
* @author 123774135@qq.com 
* @date 2019年8月7日 下午3:38:47 
* @version V1.0   
*/
package cn.com.ytst.data.integration.util;

import java.io.File;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.io.Files;

import cn.com.ytst.data.integration.exception.ClassFileNotFoundException;

/** 
* @ClassName: HttpClassLoad 
* @Description: 自定义类加载器,需要重写findClass方法
* @author 123774135@qq.com 
* @date 2019年8月7日 下午3:38:47 
*  
*/
public class HttpClassLoad extends ClassLoader{

	private Logger logger = LoggerFactory.getLogger(HttpClassLoad.class);
	
	
	private String path;
	
	private String filname;
	
	public  HttpClassLoad(String path,String filname){
		this.path = path;
		this.filname = filname;
	}
	
	
	public String getPath() {
		return path;
	}


	public void setPath(String path) {
		this.path = path;
	}

	public String getFilname() {
		return filname;
	}


	public void setFilname(String filname) {
		this.filname = filname;
	}


	public Class<?> findClass(String name) {
		byte [] b = loadClass();
		if(null == b) {
			throw new NullPointerException("改文件内容不合法或者为空!");
		}
		 return defineClass(name, b, 0, b.length);
	}
	
	/**
	 * 
	* @Title: loadClass 
	* @Description: 
	* @param @return    设定文件 
	* @return byte[]    返回类型 
	* @throws
	 */
	private byte[] loadClass() {
		String pathname = this.path+File.separator+this.filname;
		File file = new File(pathname);
		if(file.exists()) {
			throw new ClassFileNotFoundException("改文件没有发现!");
		}
		try {
			return Files.toByteArray(file);
		} catch (IOException e) {
			logger.error("",e);
		}
		return null;
	}
}
